// Learn TypeScript:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html

import ComponentBase from "../TS/BASE/ComponentBase";

const {ccclass, property} = cc._decorator;

@ccclass
export default class YP extends cc.Component{
    @property([cc.AudioClip])
    public Byp: cc.AudioClip[] = []; // 存储 BGM 资源
    private Bte: number[] = []; // 存储播放的音频的 ID，用于暂停播放的音频

    @property([cc.AudioClip])
    public Nyp:cc. AudioClip[] = []; // 存储自然音效资源
    private Nte: number[] = []; // 存储播放的音频的 ID，用于暂停播放的音频

    @property([cc.AudioClip])
    public Ayp: cc.AudioClip[] = []; // 存储攻击音效资源

    @property([cc.AudioClip])
    public Bhyp:cc. AudioClip[] = []; // 存储受击音效资源

    @property([cc.AudioClip])
    public Myp: cc.AudioClip[] = []; // 存储法术音效资源

    @property([cc.AudioClip])
    public Uiyp: cc.AudioClip[] = []; // 存储 UI 音效资源

    @property([cc.AudioClip])
    public Qtyp: cc.AudioClip[] = []; // 存储其他音效资源

  
  
  
  
  ZJP(n: string, s: boolean, t: number) {
    let audioClip: cc.AudioClip | null = null;
    let audioId: number | null = null;
let o=0.8
    switch (n) {
        case "byp":
            audioClip = this.Byp[t];
            break;
        case "ayp":
            audioClip = this.Ayp[t];
            break;
        case "bhyp":
            audioClip = this.Bhyp[t];
            break;
        case "myp":
            audioClip = this.Myp[t];
            break;
        case "uyp":
            audioClip = this.Uiyp[t];o=0.3
            break;
        case "nyp":
            audioClip = this.Nyp[t];
            break;
        case "qyp":
            audioClip = this.Qtyp[t];
            break;
    }

  
  audioId=  cc.audioEngine.play(audioClip, s, 1);
  
  cc.audioEngine.setVolume(audioId, o);
}
  
  
  
  
  
  
  
    PlayAudioclip(n: string, s: boolean, t: number) {
        let audioClip: cc.AudioClip | null = null;
        let audioId: number | null = null;

        switch (n) {
            case "byp":
                audioClip = this.Byp[t];
                break;
            case "ayp":
                audioClip = this.Ayp[t];
                break;
            case "bhyp":
                audioClip = this.Bhyp[t];
                break;
            case "myp":
                audioClip = this.Myp[t];
                break;
            case "uiyp":
                audioClip = this.Uiyp[t];
                break;
            case "nyp":
                audioClip = this.Nyp[t];
                break;
            case "qtyp":
                audioClip = this.Qtyp[t];
                break;
        }

     
          this.fadeInAudioclip(audioClip, 2,s); // 渐入效果，默认持续 1 秒
        }
    

    // 暂停循环播放的音频
    PauseAudioclip(n: string, t: number) {
        let audioId: number | null = null;
        let audioClip: cc.AudioClip | null = null;
        switch (n) {
            case "byp":
                audioClip = this.Byp[t];
                break;
            case "ayp":
                audioClip = this.Ayp[t];
        }
console.log(audioId)
  
            this.fadeOutAudioclip( audioClip,3); // 渐出效果，默认持续 1 秒
       
    }

    private audioIds: { [key: string]: number } = {};

    // 渐入效果
    fadeInAudioclip(audioClip: cc.AudioClip, duration: number,k) {
        if (this.audioIds[audioClip.url]) {
            // 如果音频已经在播放，先停止
           cc. audioEngine.stop(this.audioIds[audioClip.url]);
        }
        // 播放音频，初始音量为 0
        const audioId =cc. audioEngine.play(audioClip,k, 0);
        this.audioIds[audioClip.url] = audioId; // 存储音频 ID

        // 渐入效果的调度器
        let currentVolume = 0;
        const targetVolume = 10;
        const increment = targetVolume / (duration * 60); // 60 是每秒的帧数

        const incrementVolume = () => {
            currentVolume += increment;
            if (currentVolume > targetVolume) {
                currentVolume = targetVolume;
            }
           cc. audioEngine.setVolume(audioId, currentVolume);

            if (currentVolume < targetVolume) {
                this.scheduleOnce(incrementVolume, 1 / 60);
            }
        };

        incrementVolume(); // 开始渐入
    }

    // 渐出效果
    fadeOutAudioclip(audioClip: cc.AudioClip, duration: number) {
        const audioId = this.audioIds[audioClip.url];
        if (audioId) {
         //   let currentVolume = 1;
          //  const decrement = currentVolume / (duration * 60);

          //  const decrementVolume = () => {
            //    currentVolume -= decrement;
           //     if (currentVolume < 0) {
            //        currentVolume = 0;
            //    }
            //    cc.audioEngine.setVolume(audioId, currentVolume/1000);

            //    if (currentVolume > 0) {
             //       this.scheduleOnce(decrementVolume, 1 / 60);
              //  } else {
                   cc.audioEngine.stop(audioId); // 停止音频播放
                    delete this.audioIds[audioClip.url]; // 移除音频 ID
                }
          //  };

           // decrementVolume(); // 开始渐出
        //}
    }

    // 停止所有音频
    stopAllAudio() {
        for (const audioUrl in this.audioIds) {
            const audioId = this.audioIds[audioUrl];
           cc. audioEngine.stop(audioId);
        }
        this.audioIds = {}; // 清空音频 ID 映射
    }

    start() {
   
    }
}